package com.edu.controller.front;

import com.JwtUtils;
import com.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.client.OrderClient;
import com.edu.entity.Course;
import com.edu.entity.Teacher;
import com.edu.entity.chapter.ChapterVo;
import com.edu.entity.frontvo.CourseFrontVo;
import com.edu.entity.frontvo.CourseWebVo;
import com.edu.service.ChapterService;
import com.edu.service.CourseService;
import com.edu.service.TeacherService;
import com.ordervo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

	@Autowired
	private CourseService courseService;

	@Autowired
	private ChapterService chapterService;

	@Autowired
	private OrderClient orderClient;

	//1 条件查询带分页查询课程
	@PostMapping("/getFrontCourseList/{page}/{limit}")
	public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
								@RequestBody(required = false) CourseFrontVo courseFrontVo){
		Page<Course> coursePage = new Page<>(page, limit);
		Map<String,Object> map = courseService.getFrontCourseList(coursePage,courseFrontVo);
		return R.ok().data(map);
	}

	@GetMapping("/getFrontCourseInfo/{courseId}")
	//此处有bug, 无法从前端获取token,然后执行了 熔断机制的false
	public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
		//根据课程id， 编写sql语句查询课程信息
		CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

		//根据课程id， 编写sql语句查询章节小节信息
		List<ChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);

		//根据课程id 和用户id 查询当前课程是否已支付
		String token = JwtUtils.getMemberIdByJwtToken(request);
		System.out.println(token);
		boolean buyCourse = orderClient.isBuyCourse(courseId, JwtUtils.getMemberIdByJwtToken(request));
		System.out.println("=============buyCourse: ---"+buyCourse);
		return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);

	}

	@PostMapping("/getCourseInfoOrder/{id}")
	public CourseWebVoOrder getCourseInfoOrder(@PathVariable("id") String id){
		CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
		CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
		BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
		return courseWebVoOrder;
	}
}
